<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>买卖股票的最佳时机 - 算法详解</title>
    <link rel="stylesheet" href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css">
    <link rel="stylesheet" href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
            min-height: 100vh;
        }
        .hero-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }
        .card-hover {
            transition: all 0.3s ease;
        }
        .card-hover:hover {
            transform: translateY(-5px);
            box-shadow: 0 20px 40px rgba(0,0,0,0.1);
        }
        .code-block {
            background: #282c34;
            color: #abb2bf;
            border-radius: 8px;
            overflow: hidden;
        }
        .code-block pre {
            padding: 1.5rem;
            overflow-x: auto;
        }
        .keyword { color: #c678dd; }
        .function { color: #61afef; }
        .string { color: #98c379; }
        .number { color: #d19a66; }
        .comment { color: #5c6370; font-style: italic; }
        .drop-cap {
            float: left;
            font-size: 4rem;
            line-height: 1;
            font-weight: 700;
            margin-right: 0.5rem;
            color: #667eea;
            font-family: 'Noto Serif SC', serif;
        }
        .mermaid {
            display: flex;
            justify-content: center;
            margin: 2rem 0;
        }
        @keyframes fadeIn {
            from { opacity: 0; transform: translateY(20px); }
            to { opacity: 1; transform: translateY(0); }
        }
        .fade-in {
            animation: fadeIn 0.8s ease-out;
        }
    </style>
</head>
<body>
    <!-- Hero Section -->
    <section class="hero-gradient text-white py-20 px-6">
        <div class="max-w-6xl mx-auto text-center fade-in">
            <h1 class="text-5xl md:text-6xl font-bold mb-6">
                <i class="fas fa-chart-line mr-4"></i>买卖股票的最佳时机
            </h1>
            <p class="text-xl md:text-2xl opacity-90 max-w-3xl mx-auto">
                掌握贪心算法的精髓，一次遍历找到最大利润
            </p>
            <div class="mt-10 flex justify-center space-x-6">
                <div class="bg-white bg-opacity-20 rounded-lg px-6 py-3">
                    <i class="fas fa-clock mr-2"></i>时间复杂度: O(n)
                </div>
                <div class="bg-white bg-opacity-20 rounded-lg px-6 py-3">
                    <i class="fas fa-memory mr-2"></i>空间复杂度: O(1)
                </div>
            </div>
        </div>
    </section>

    <!-- Main Content -->
    <main class="max-w-6xl mx-auto px-6 py-12">
        <!-- Problem Description -->
        <section class="bg-white rounded-2xl shadow-xl p-8 mb-8 card-hover fade-in">
            <h2 class="text-3xl font-bold mb-6 text-gray-800">
                <i class="fas fa-book-open mr-3 text-purple-600"></i>题目描述
            </h2>
            <div class="prose prose-lg max-w-none">
                <p class="text-gray-700 leading-relaxed">
                    <span class="drop-cap">给</span>定一个数组，其中第 i 个元素是第 i 天的股票价格。如果你最多只允许完成一笔交易（即买入和卖出一支股票一次），设计一个算法来计算你所能获取的最大利润。
                </p>
                <div class="bg-yellow-50 border-l-4 border-yellow-400 p-4 my-6">
                    <p class="text-gray-700">
                        <i class="fas fa-exclamation-triangle mr-2 text-yellow-600"></i>
                        <strong>注意：</strong>你不能在买入股票前卖出股票。
                    </p>
                </div>
            </div>
        </section>

        <!-- Example -->
        <section class="bg-gradient-to-r from-purple-50 to-pink-50 rounded-2xl p-8 mb-8 card-hover fade-in">
            <h3 class="text-2xl font-bold mb-6 text-gray-800">
                <i class="fas fa-lightbulb mr-3 text-yellow-500"></i>示例
            </h3>
            <div class="grid md:grid-cols-2 gap-6">
                <div class="bg-white rounded-lg p-6">
                    <h4 class="font-semibold text-gray-700 mb-3">输入</h4>
                    <code class="text-lg bg-gray-100 px-4 py-2 rounded block">
                        [7, 1, 5, 3, 6, 4]
                    </code>
                </div>
                <div class="bg-white rounded-lg p-6">
                    <h4 class="font-semibold text-gray-700 mb-3">输出</h4>
                    <code class="text-lg bg-gray-100 px-4 py-2 rounded block">
                        5
                    </code>
                    <p class="text-sm text-gray-600 mt-3">
                        第2天买入（价格=1），第5天卖出（价格=6），利润=5
                    </p>
                </div>
            </div>
        </section>

        <!-- Algorithm Visualization -->
        <section class="bg-white rounded-2xl shadow-xl p-8 mb-8 card-hover fade-in">
            <h2 class="text-3xl font-bold mb-6 text-gray-800">
                <i class="fas fa-project-diagram mr-3 text-blue-600"></i>算法可视化
            </h2>
            <div class="mermaid">
                graph LR
                    A[开始遍历] --> B{更新最小价格}
                    B --> C[计算当前利润]
                    C --> D{是否最大利润?}
                    D -->|是| E[更新最大利润]
                    D -->|否| F[继续]
                    E --> G{遍历结束?}
                    F --> G
                    G -->|否| B
                    G -->|是| H[返回最大利润]
                    
                    style A fill:#667eea,stroke:#fff,color:#fff
                    style H fill:#48bb78,stroke:#fff,color:#fff
                    style B fill:#ed8936,stroke:#fff,color:#fff
                    style D fill:#ed8936,stroke:#fff,color:#fff
            </div>
        </section>

        <!-- Core Algorithm -->
        <section class="bg-white rounded-2xl shadow-xl p-8 mb-8 card-hover fade-in">
            <h2 class="text-3xl font-bold mb-6 text-gray-800">
                <i class="fas fa-brain mr-3 text-green-600"></i>核心算法
            </h2>
            <div class="grid md:grid-cols-3 gap-6 mb-8">
                <div class="text-center p-6 bg-gradient-to-br from-blue-50 to-blue-100 rounded-lg">
                    <i class="fas fa-chart-area text-4xl text-blue-600 mb-4"></i>
                    <h3 class="font-bold text-lg mb-2">贪心算法</h3>
                    <p class="text-gray-700">每一步都做出当前最优选择</p>
                </div>
                <div class="text-center p-6 bg-gradient-to-br from-green-50 to-green-100 rounded-lg">
                    <i class="fas fa-sync-alt text-4xl text-green-600 mb-4"></i>
                    <h3 class="font-bold text-lg mb-2">单遍扫描</h3>
                    <p class="text-gray-700">只需遍历一次数组</p>
                </div>
                <div class="text-center p-6 bg-gradient-to-br from-purple-50 to-purple-100 rounded-lg">
                    <i class="fas fa-tachometer-alt text-4xl text-purple-600 mb-4"></i>
                    <h3 class="font-bold text-lg mb-2">高效实现</h3>
                    <p class="text-gray-700">O(n)时间，O(1)空间</p>
                </div>
            </div>
        </section>

        <!-- Solution -->
        <section class="bg-white rounded-2xl shadow-xl p-8 mb-8 card-hover fade-in">
            <h2 class="text-3xl font-bold mb-6 text-gray-800">
                <i class="fas fa-code mr-3 text-indigo-600"></i>解题思路
            </h2>
            <div class="space-y-4">
                <div class="flex items-start">
                    <span class="flex-shrink-0 w-10 h-10 bg-purple-600 text-white rounded-full flex items-center justify-center font-bold mr-4">1</span>
                    <div>
                        <h4 class="font-semibold text-lg mb-2">维护两个关键变量</h4>
                        <p class="text-gray-700">
                            <code class="bg-gray-100 px-2 py-1 rounded">min_price</code> 记录到目前为止的最低价格，
                            <code class="bg-gray-100 px-2 py-1 rounded">max_profit</code> 记录最大利润。
                        </p>
                    </div>
                </div>
                <div class="flex items-start">
                    <span class="flex-shrink-0 w-10 h-10 bg-purple-600 text-white rounded-full flex items-center justify-center font-bold mr-4">2</span>
                    <div>
                        <h4 class="font-semibold text-lg mb-2">遍历价格数组</h4>
                        <p class="text-gray-700">
                            对于每个价格，更新最小价格，并计算如果在当天卖出的利润。
                        </p>
                    </div>
                </div>
                <div class="flex items-start">
                    <span class="flex-shrink-0 w-10 h-10 bg-purple-600 text-white rounded-full flex items-center justify-center font-bold mr-4">3</span>
                    <div>
                        <h4 class="font-semibold text-lg mb-2">更新最大利润</h4>
                        <p class="text-gray-700">
                            如果当前利润大于已记录的最大利润，则更新最大利润值。
                        </p>
                    </div>
                </div>
            </div>
        </section>

        <!-- Code Implementation -->
        <section class="bg-white rounded-2xl shadow-xl p-8 card-hover fade-in">
            <h2 class="text-3xl font-bold mb-6 text-gray-800">
                <i class="fas fa-laptop-code mr-3 text-red-600"></i>代码实现
            </h2>
            <div class="code-block">
                <pre><code><span class="keyword">def</span> <span class="function">maxProfit</span>(prices):
    <span class="comment"># 初始化最小价格为无穷大</span>
    min_price = <span class="function">float</span>(<span class="string">'inf'</span>)
    <span class="comment"># 初始化最大利润为0</span>
    max_profit = <span class="number">0</span>
    
    <span class="comment"># 遍历每一天的价格</span>
    <span class="keyword">for</span> price <span class="keyword">in</span> prices:
        <span class="comment"># 更新最小价格</span>
        min_price = <span class="function">min</span>(min_price, price)
        <span class="comment"># 计算当前利润并更新最大利润</span>
        max_profit = <span class="function">max</span>(max_profit, price - min_price)
    
    <span class="keyword">return</span> max_profit</code></pre>
            </div>
        </section>

        <!-- Additional Tips -->
        <section class="mt-12 bg-gradient-to-r from-indigo-500 to-purple-600 rounded-2xl p-8 text-white fade-in">
            <h3 class="text-2xl font-bold mb-4">
                <i class="fas fa-star mr-3"></i>算法要点总结
            </h3>
            <div class="grid md:grid-cols-2 gap-6">
                <div>
                    <h4 class="font-semibold text-lg mb-2">
                        <i class="fas fa-check-circle mr-2"></i>关键思想
                    </h4>
                    <p class="opacity-90">
                        贪心策略：总是记录到目前为止的最低买入价格，并计算在每个时间点卖出的最大利润。
                    </p>
                </div>
                <div>
                    <h4 class="font-semibold text-lg mb-2">
                        <i class="fas fa-rocket mr-2"></i>优化技巧
                    </h4>
                    <p class="opacity-90">
                        只需